package com.wondersgroup.insurance.claimlib.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Toast;


public class BaseActivity extends AppCompatActivity {
    final String ERR_MSG = "err_msg";
    public final int RETURN_CODE_OK = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    public void initParams(){

    }

    public void initView(){
        findViews();
    }

    public void findViews(){

    }
    public void initListener(){

    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelToast();
    }

    @Override
    protected void onDestroy() {
        destoryRes();
        super.onDestroy();
    }

    public void destoryRes(){
        dismissProgressDialog();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK&& event.getAction() == KeyEvent.ACTION_DOWN){
            exitFunction(RETURN_CODE_OK, null);
            return true;
        }else {
            return super.onKeyDown(keyCode, event);

        }
    }


    public void exitFunction(int code, String msg) {
        exit(code,msg);
    }


    public void exit(int code, String msg) {
        Intent data = new Intent();
        data.putExtra(ERR_MSG, msg == null ? "" : msg);
        setResult(code,data);
        finish();
    }



    ProgressDialog mLoadingDialog;

    public void showProgressDialog(String msg){
        if (null == mLoadingDialog && this != null){
            mLoadingDialog = new ProgressDialog(this);
            mLoadingDialog.setCancelable(false);
        }
        if (mLoadingDialog != null){
            mLoadingDialog.setMessage(msg == null? "" : msg);
            mLoadingDialog.show();
        }

    }


    public void dismissProgressDialog(){
        if (null != mLoadingDialog){
            mLoadingDialog.dismiss();
        }
    }

    Toast toast = null;

    /**
     *
     * @param msg
     * @param longOrShort
     */
    public  void showToast( String msg, boolean longOrShort){
        if (TextUtils.isEmpty(msg))
            return;
        if (toast != null){
            toast.cancel();
        }
         toast = Toast.makeText(getApplicationContext(), msg, longOrShort ? Toast.LENGTH_LONG :Toast.LENGTH_SHORT );
//		toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

    }

    /**
     *
     */
    public  void cancelToast(){
        if (toast != null){
            toast.cancel();
            toast = null;
        }
    }

}
